package utils;

import java.util.Properties;

import javax.mail.Folder;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Store;

public class LeeGmail2 {
	
	public LeeGmail2(){
	  }
	
	public String getCantidadMailsNoLeidos(String user,String pass)
    throws Exception {

     Properties props = System.getProperties();
     user = user+"@siat.unrc.edu.ar";
		props.setProperty("mail.store.protocol", "imaps");
			try {
				Session session = Session.getDefaultInstance(props, null);
				Store store = session.getStore("imaps");
				store.connect("imap.gmail.com", user, pass);
				//System.out.println(store);

				Folder inbox = store.getFolder("Inbox");
				inbox.open(Folder.READ_ONLY);
				//System.out.println("total = "+inbox.getNewMessageCount()+" - "+inbox.getUnreadMessageCount());
				//String nuevos = String.valueOf(inbox.getNewMessageCount());
				String noLeidos = String.valueOf(inbox.getUnreadMessageCount());
			
				inbox.close(true);
				store.close();
				
  			    return noLeidos;

		} catch (NoSuchProviderException e) 
		{
		 e.printStackTrace();
		 return "--";
		} catch (MessagingException e) 
		{
		 e.printStackTrace();
		 return "--";
    	}

     
     
	}

	public static void main(String args[]) {
		
      LeeGmail2 lg = new LeeGmail2();
      try {
     System.out.println("-->>>"+lg.getCantidadMailsNoLeidos("test@siat.unrc.edu.ar", ""));
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	
	}
	
	
}